*This file runs the occupational structure estimations at the local level.


*Import occupational structure balanced sample
use "$path/occupational_structure_data", clear

*Local shift share shocks
joinby code using  "$path/bartik_shocks.dta", unmatched(none)

preserve
keep if year==1940
gen manushare_a1940=manushare_a
gen agrishare_a1940=agrishare_a
gen ln_pop_1940=ln_pop
keep code manushare_a1940 agrishare_a1940  ln_pop_1940
tempfile controls
save `controls'
restore

joinby code using `controls'

tab year, gen(y_)

gen bartik_1920=bartik_st*y_1
gen bartik_1930=bartik_st*y_2
gen bartik_1940=bartik_st*y_3
gen bartik_1950=bartik_st*y_4
gen bartik_1960=bartik_st*y_5
gen bartik_1970=bartik_st*y_6

gen post=(year>1940)
gen bartik_post=bartik_st*post

gen bartik_h_post=bartik_st_H*post
gen bartik_l_post=bartik_st_L*post
gen bartik_ntp_st_post=post*bartik_ntp_st

*Define control variables
global baseline2 year#c.manushare_a1940 year#c.agrishare_a1940
global enet_11  c.agrishare_a1940#year  c.manushare_a1940#year c.income_tax_payers38#year 

foreach x in manushare_a   agrishare_a  {
reghdfe `x'  bartik_post [aw=wei], a(year#laani50  $baseline2 year#town year#city code) cl(code)
estadd local enet " "
estadd local Municipalities " 356" 
sum `x'
estadd scalar ymean=r(mean)
estadd local established "Yes"

estimates store a`x' 
}

foreach x in manushare_a   agrishare_a  {
reghdfe `x'  bartik_post [aw=wei], a(year#laani50  $enet_11 year#town year#city code) cl(code)
estadd local enet "\checkmark"
estadd local Municipalities " 356" 
sum `x'
estadd scalar ymean=r(mean)
estadd local established "Yes"

estimates store b`x' 
}

*Differences by high skill and low skill exposure

foreach x in manushare_a   agrishare_a  {
reghdfe `x'  bartik_h_post bartik_l_post [aw=wei], a(year#laani50  $baseline2 year#town year#city code) cl(code)
estadd local enet " "
estadd local Municipalities " 356" 
sum `x'
estadd scalar ymean=r(mean)
estimates store ahl`x' 
estadd local established "Yes"
}


foreach x in manushare_a   agrishare_a  {
reghdfe `x'  bartik_h_post bartik_l_post [aw=wei], a(year#laani50  $enet_11 year#town year#city code) cl(code)
estadd local enet "\checkmark"
estadd local Municipalities "356" 
sum `x'
estadd scalar ymean=r(mean)
estimates store bhl`x' 
estadd local established "Yes"
}

*Without established industries
preserve 
replace bartik_post=bartik_ntp_st_post
foreach x in manushare_a   agrishare_a  {
reghdfe `x'  bartik_post [aw=wei], a(year#laani50  $baseline2 year#town year#city code) cl(code)
estadd local enet " "
estadd local Municipalities "356" 
estadd local established "No"

sum `x'
estadd scalar ymean=r(mean)
estimates store antp`x' 
}
restore 

preserve 
replace bartik_post=bartik_ntp_st_post
foreach x in manushare_a   agrishare_a  {
reghdfe `x'  bartik_post [aw=wei], a(year#laani50  $enet_11 year#town year#city code) cl(code)
estadd local enet "\checkmark"
estadd local Municipalities "356" 
estadd local established "No"

sum `x'
estadd scalar ymean=r(mean)
estimates store bntp`x' 
}
restore 

label var bartik_post "Exposure x Post"
label var bartik_h_post "HS Exposure x Post"
label var bartik_l_post "LS Exposure x Post"

esttab amanushare_a bmanushare_a bntpmanushare_a bhlmanushare_a  aagrishare_a bagrishare_a bntpagrishare_a bhlagrishare_a    aln_workersmean bln_workersmean  bntpln_workersmean bhlln_workersmean  aln_factorynumber bln_factorynumber bntpln_factorynumber bhlln_factorynumber     ///
using   "$tables/DD_estimates_municipality.tex",  ///
mgroups("Manufacturing Employment Share" "Agriculture Employment Share" "ln Manufacturing Workers" "ln Establishments"  ,   pattern(1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0)  prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}) ) ///
replace  keep(bartik_post  bartik_h_post bartik_l_post) nonotes ///
b(%9.3f) se(%9.3f) stats(N Municipalities established enet, fmt(0 3) layout(@ @ @) ///
labels("N" "Municipalities" "Established" "E-net controls"  ///
)) ///
label star(* .1 ** .05 *** .01) nodepvars nomtitle 


*Create event study Figure 5 panel B

tempfile t1

parmby  "reghdfe manushare_a  bartik_1920 bartik_1930  bartik_1950 bartik_1960 bartik_1970 [aw=wei] , a(year#laani50 year#city year#town code $enet_11) cl(code)", lab saving(`"`t1'"',replace) idn(1) ids(Unadjusted)

preserve

clear all
set obs 1
gen year =1940
gen estimate = 0
gen parm ="bartikX1940"
gen min95 =0
gen max95 =0
append using `t1'
*keep if regexm(parm, "^repa*")
gen yearstr =substr(parm, -4,4)
destring yearstr, replace force
sort yearstr
drop if parm=="_cons"

 
twoway (scatter estimate yearstr , msize(medium) mcolor(dknavy))  (rspike   min95 max95 yearstr , lcolor(dknavy)),  xsize(6) ysize(4) xtitle(Year, ) xline(1944, lcolor(maroon)) xline(1952, lcolor(maroon)) title("Panel A")  ///
  xlabel(1920 1930 1940 1950 1960 1970) plotregion(lcolor(black)) graphregion(lcolor(black) fcolor(white) color(white)) legend(off)  xlabel(#8)  xlabel(,grid nogextend ) ylabel(,grid nogextend)  ytitle("Estimates of Manufacturing Employment Share", margin(0 1 0 0))   saving("$graphs/manushare.gph", replace)

twoway  (rspike   min95 max95 yearstr , lcolor(gs10)) (scatter estimate yearstr , msize(medium) mcolor(gs0)) ,  xsize(6) ysize(4) xtitle(Year, ) xline(1944, lcolor(gs10)) xline(1952, lcolor(gs10)) title("Panel A")  ///
  xlabel(1920 1930 1940 1950 1960 1970) plotregion(lcolor(black)) graphregion(lcolor(black) fcolor(white) color(white)) legend(off)  xlabel(#8)  xlabel(,grid nogextend ) ylabel(,grid nogextend)  ytitle("Estimates of Manufacturing Employment Share", margin(0 1 0 0))   saving("$graphs/manushare_bw.gph", replace)
restore

*Robustness checks

global controls40 c.relative_tractors1940#year  c.displaced_share_1940#year c.ln_pop_1940#year c.swedish_share_1940#year  c.share_service_1940#year c.relative_tractors1940#year c.relative_cows1940#year c.ln_arable_1940#year c.income_tax_payers38#year 

local repaflexible1 "bartik_post"
local repaflexible2 "  bartik_1920 bartik_1930  bartik_1950 bartik_1960 bartik_1970 "

foreach x in manushare_a   agrishare_a {
reghdfe `x' `repaflexible1' [aw=wei], a($baseline2 year#city year#town year#laani50    code year) cl(code)
estadd local controls ""
estadd local baseline "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "356" 
sum `x'
estadd scalar ymean=r(mean)
estimates store f`x' 
}

foreach x in manushare_a   agrishare_a {
reghdfe `x' `repaflexible2' [aw=wei], a($baseline2  year#city year#town year#laani50    code year) cl(code)
estadd local controls ""
estadd local baseline "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "356" 
sum `x'
estadd scalar ymean=r(mean)
estimates store f1`x' 
}

local repaflexible1 "bartik_post"
local repaflexible2 "  bartik_1920 bartik_1930  bartik_1950 bartik_1960 bartik_1970 "

foreach x in manushare_a   agrishare_a {
reghdfe `x' `repaflexible1' [aw=wei], a($enet_11 year#city year#town year#laani50  code year) cl(code)
estadd local controls ""
estadd local enet "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "356" 
sum `x'
estadd scalar ymean=r(mean)
estimates store d`x' 
}

foreach x in manushare_a   agrishare_a {
reghdfe `x' `repaflexible2' [aw=wei], a($enet_11 year#city year#town year#laani50  code year) cl(code)
estadd local controls ""
estadd local enet "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "356" 
sum `x'
estadd scalar ymean=r(mean)
estimates store d1`x' 
}


local repaflexible1 "bartik_post"
local repaflexible2 "  bartik_1920 bartik_1930  bartik_1950 bartik_1960 bartik_1970 "

foreach x in manushare_a   agrishare_a {
reghdfe `x' `repaflexible1' [aw=wei], a($enet_11 year#city year#town  code year) cl(code)
estadd local controls ""
estadd local enet "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "356" 
sum `x'
estadd scalar ymean=r(mean)
estimates store g`x' 
}

foreach x in manushare_a   agrishare_a {
reghdfe `x' `repaflexible2' [aw=wei], a($enet_11 year#city year#town  code year) cl(code)
estadd local controls ""
estadd local enet "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "356" 
sum `x'
estadd scalar ymean=r(mean)
estimates store g1`x' 
}

local repaflexible1 "bartik_post"
local repaflexible2 "  bartik_1920 bartik_1930  bartik_1950 bartik_1960 bartik_1970 "

foreach x in manushare_a   agrishare_a {
reghdfe `x' `repaflexible1' [aw=wei], a($controls40 $baseline2  year#city year#town year#laani50  code year) cl(code)
estadd local controls "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "356" 
sum `x'
estadd scalar ymean=r(mean)
estimates store h`x' 
}

foreach x in manushare_a   agrishare_a {
reghdfe `x' `repaflexible2' [aw=wei], a($controls40 $baseline2  year#city year#town year#laani50  code year) cl(code)
estadd local controls "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "356" 
sum `x'
estadd scalar ymean=r(mean)
estimates store h1`x' 
}

save "$path/elinkeino_forregs.dta", replace
joinby code using "$path/industry_controls", unmatched(master) 

*Define industial survey controls
global industry   c.ln_workersmean_1943#year c.ln_factorynumber_1943#year c.ln_otherpersonnel_1943#year

local repaflexible1 "bartik_post"
local repaflexible2 "  bartik_1920 bartik_1930  bartik_1950 bartik_1960 bartik_1970 "

foreach x in manushare_a   agrishare_a {
reghdfe `x' `repaflexible1' [aw=wei], a( $industry $baseline2  year#city year#town year#laani50  code year) cl(code)
estadd local baseline "\checkmark"
estadd local industry1 "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "196" 
sum `x'
estadd scalar ymean=r(mean)
estimates store v`x' 
}

foreach x in manushare_a   agrishare_a {
reghdfe `x' `repaflexible2' [aw=wei], a( $industry $baseline2  year#city year#town year#laani50  code year) cl(code)
estadd local baseline "\checkmark"
estadd local industry1 "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "196" 
sum `x'
estadd scalar ymean=r(mean)
estimates store v1`x' 
}

local repaflexible1 "bartik_post"
local repaflexible2 "  bartik_1920 bartik_1930  bartik_1950 bartik_1960 bartik_1970 "

foreach x in manushare_a   agrishare_a {
reghdfe `x' `repaflexible1' [aw=wei], a($controls40 $industry $baseline2  year#city year#town year#laani50  code year) cl(code)
estadd local controls "\checkmark"
estadd local industry1 "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "196" 
sum `x'
estadd scalar ymean=r(mean)
estimates store b`x' 
}

foreach x in manushare_a   agrishare_a {
reghdfe `x' `repaflexible2' [aw=wei], a($controls40 $industry $baseline2  year#city year#town year#laani50  code year) cl(code)
estadd local controls "\checkmark"
estadd local industry1 "\checkmark"
estadd local lfe "\checkmark"
estadd local ufe "" 
estadd local kunta "196" 
sum `x'
estadd scalar ymean=r(mean)
estimates store b1`x' 
}

label var bartik_1920 "1920 x Exposure"
label var bartik_1930 "1930 x Exposure"
label var bartik_1950 "1950 x Exposure"
label var bartik_1960 "1960 x Exposure"
label var bartik_1970 "1970 x Exposure"

esttab fmanushare_a f1manushare_a   dmanushare_a d1manushare_a gmanushare_a g1manushare_a hmanushare_a h1manushare_a vmanushare_a v1manushare_a  bmanushare_a b1manushare_a   fagrishare_a f1agrishare_a dagrishare_a d1agrishare_a gagrishare_a g1agrishare_a hagrishare_a h1agrishare_a vagrishare_a v1agrishare_a bagrishare_a b1agrishare_a ///
using   "$tables/DD_flexible_estimates_municipality_robust_1.tex",  ///
mgroups("Manufacturing Employment Share" "Agriculture Employment Share"  ,   pattern(1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}) ) ///
replace  keep(`repaflexible1' `repaflexible2') nonotes ///
b(%9.3f) se(%9.3f) stats(N kunta baseline  lfe enet  industry1 controls, fmt(0 3) layout(@ @ @ @) ///
labels("N" "Municipalities" "Baseline controls" "Region FE"  "Elastic net controls"  "1943 Industry controls" "1940 municipal controls"   ///
)) ///
label star(* .1 ** .05 *** .01) nodepvars nomtitle 
